
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.MyMessage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 获取消息列表请求类
 * 
 * @author ljc
 * @since 2013-12-24 下午4:54:22
 */
public class MessageListTask {
    private int pageIndex;

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 获取消息列表请求接口
     * 
     * @return
     */
    public MessageListHttpResponse request(String Username, int Page, int Pagesize, String unixTime) {
        pageIndex = Page;
        try {
            ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
            // 生成请求包
            HttpBaseRequestPackage req = new HttpBaseRequestPackage();
            req.setUserName(Username);
            // 生成响应包
            ResponsePackage resp = new MessageListResponsePackage();
            // 生成响应类
            MessageListHttpResponse appsHttpResponse = new MessageListHttpResponse();
            // 填充参数，key-value。key是接口要求传的变量名称
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("cmd", ProtocolCmd.CMD_MESSAGE_LIST);
            params.put("username", Username);
            params.put("page", Page);
            params.put("pagesize", Pagesize);
            params.put("encrypt", 1);
            params.put("newesttime", unixTime);
            requestParams.add(params);
            req.setRequestParams(requestParams);
            mCmdList.add(ProtocolCmd.CMD_MESSAGE_LIST);
            KGHttpClient.request(req, resp);
            // 响应类赋值
            resp.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    class MessageListResponsePackage extends HttpBaseResponsePackage<MessageListHttpResponse> {

        @Override
        protected void handleResponse(MessageListHttpResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "MessageListTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    response.setCode(jsonResp.optString("code"));
                    response.setMessage(jsonResp.optString("message"));
                    response.setPrompt(jsonResp.optString("prompt"));
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_MESSAGE_LIST.equalsIgnoreCase(cmd)) {
                        ArrayList<MyMessage> messageList = new ArrayList<MyMessage>();
                        JSONArray jsonResult = jsonResp.getJSONArray("data");
                        if (jsonResult != null) {
                            for (int j = 0; j < jsonResult.length(); j++) {
                                JSONObject o = jsonResult.getJSONObject(j);
                                MyMessage message = new MyMessage();
                                message.setClassid(o.getInt("classid"));
                                // 内容是是否有加密
                                int isEncrypted = o.getInt("is_encrypted");
                                if (isEncrypted == 1) {
                                    message.setContent(AppUtil.desDecrypt(o.getString("content")));
                                } else {
                                    message.setContent(o.getString("content"));
                                }
                                message.setIsread(o.getInt("isread"));
                                message.setMessageid(o.getInt("messageid"));
                                message.setTitle(o.getString("title"));
                                if (!o.isNull("inserttime")) {
                                    message.setInserttime(o.getString("inserttime"));
                                }
                                message.setPageIndex(pageIndex);
                                messageList.add(message);
                            }
                            response.setMessageList(messageList);
                        }
                    }
                }
                response.setOk(true);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 响应类，这个类主要用于存储返回数据中的各个字段。
     * 
     * @author ljc
     * @since 2013-12-24 下午5:08:43
     */
    public class MessageListHttpResponse extends ResponseMessage {

        /**
         * 
         */
        private static final long serialVersionUID = -8098413854753887939L;

        private ArrayList<MyMessage> messageList = new ArrayList<MyMessage>();

        public ArrayList<MyMessage> getMessageList() {
            if (messageList == null) {
                messageList = new ArrayList<MyMessage>();
            }
            return messageList;
        }

        public void setMessageList(ArrayList<MyMessage> messageList) {
            this.messageList = messageList;
        }

    }
}
